package com.example.thread.runnable;

import com.example.thread.mapper.TestMapper;
import com.example.thread.pojo.Demo;
import com.example.thread.util.SpringContext;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class GetRunnable implements Callable<List<Demo>> {

    private Logger logger = LoggerFactory.getLogger(getClass());


    private Integer param;

    private CountDownLatch latch;

    private CountDownLatch mainLatch;

    public GetRunnable() {
    }

    public GetRunnable(Integer param, CountDownLatch latch, CountDownLatch mainLatch) {
        this.param = param;
        this.latch = latch;
        this.mainLatch = mainLatch;
    }

    public GetRunnable(Integer param) {
        this.param = param;
    }

    @Override
    public List<Demo> call() throws Exception {

        long start = System.currentTimeMillis();
        mainLatch.await();
        logger.info("starting");

        TestMapper mapper = SpringContext.getBean(TestMapper.class);
        List<Demo> demoList = mapper.getByParam(param);

        latch.countDown();
        long end = System.currentTimeMillis();

        logger.info("{}==时间=={}",Thread.currentThread().getName(), end - start);

        return demoList;
    }
}
